﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyDiem.models;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;
namespace QuanLyDiem.Controllers
{
    public class C_SinhVien
    {
        public static void LoadViewBindingSource(BindingSource bindingSource, Context db = null)
        {
            if (db == null) db = new Context();

            bindingSource.DataSource = db.HoSoSinhViens.OrderBy(a => a.ID_sv).ToList();
        }
        public static void LoadDataSource(RepositoryItemLookUpEdit lookUpedit, Context db = null)
        {
            if (db == null) db = new Context();
            lookUpedit.Columns.Clear();
            lookUpedit.DataSource = db.HoSoSinhViens.OrderBy(a => a.ID_sv).ToList();
            lookUpedit.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Ho_ten", "Họ tên"));
            lookUpedit.DisplayMember = "Ho_ten";
            lookUpedit.ValueMember = "ID_sv";
            lookUpedit.NullText = string.Empty;
            lookUpedit.NullValuePrompt = string.Empty;
            lookUpedit.NullValuePromptShowForEmptyValue = true;
            lookUpedit.AllowDropDownWhenReadOnly = DevExpress.Utils.DefaultBoolean.False;
        }
        public static void LoadDataSource1(RepositoryItemLookUpEdit lookUpedit, Context db = null)
        {
            if (db == null) db = new Context();
            lookUpedit.Columns.Clear();
            lookUpedit.DataSource = db.HoSoSinhViens.OrderBy(a => a.ID_sv).ToList();
            lookUpedit.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Ma_sv", "Mã sinh viên"));
            lookUpedit.DisplayMember = "Ma_sv";
            lookUpedit.ValueMember = "ID_sv";
            lookUpedit.NullText = string.Empty;
            lookUpedit.NullValuePrompt = string.Empty;
            lookUpedit.NullValuePromptShowForEmptyValue = true;
            lookUpedit.AllowDropDownWhenReadOnly = DevExpress.Utils.DefaultBoolean.False;
           
        }

        public static bool Delete(HoSoSinhVien sv, Context db = null)
        {
            if (db == null) db = new Context();
            var delete = db.HoSoSinhViens.SingleOrDefault(a => a.ID_sv == sv.ID_sv);
            if (delete == null) return false;
            db.HoSoSinhViens.DeleteOnSubmit(delete);
            db.SubmitChanges();
            return true;
        }
        public static bool Insert(HoSoSinhVien sv, Context db = null)
        {
            if (db == null) db = new Context();
            db.HoSoSinhViens.InsertOnSubmit(sv);
            db.SubmitChanges();
            return true;
        }

        public static bool Update(HoSoSinhVien sv, Context db = null)
        {
            if (db == null) db = new Context();
            var update = db.HoSoSinhViens.SingleOrDefault(a => a.ID_sv == sv.ID_sv);
            if (update == null) return false;
            update.Ho_ten = sv.Ho_ten;
            update.Lop = sv.Lop;
            update.Ma_sv = sv.Ma_sv;
            update.Que_quan = sv.Que_quan;
            update.Dia_chi_tt = sv.Dia_chi_tt;
            update.Ngay_sinh = sv.Ngay_sinh;
            update.ID_gioi_tinh = sv.ID_gioi_tinh;
            db.SubmitChanges();
            return true;

        }
    }
}
